<?php
/*
 * 商品
 */

namespace app\supplier\controller;
use app\BaseController;
use think\facade\Request;
use app\supplier\model\Product;
use think\facade\Config;

class ProductController extends BaseController{

    //商品列表
    public function productList(){
        $s_uid = Request::param('s_uid');
        $page = Request::param('page');
        $num = Request::param('num');
        
        $p_name = Request::param('p_name');
        $class_one = Request::param('class_one');
        $class_two = Request::param('class_two');
        $class_thr = Request::param('class_thr');
        $supplier = Request::param('supplier');
        $con_person = Request::param('con_person');
        $con_information = Request::param('con_information');
        $res_person = Request::param('res_person');
        $cost_price_min = Request::param('cost_price_min');
        $supply_price_min = Request::param('supply_price_min');
        $cost_price_max = Request::param('cost_price_max');
        $supply_price_max = Request::param('supply_price_max');
        $brand_state = Request::param('brand_state');
        $fs_state = Request::param('fs_state');
        $pre_state = Request::param('pre_state');
        $br_name = Request::param('br_name');
        $state = Request::param('state');
        $p_num = Request::param('p_num');
        $fs_up_status  = Request::param('fs_up_status');
        $brand_up_status  = Request::param('brand_up_status');
        $pre_up_status  = Request::param('pre_up_status');
        
        $order_paytime_sta  = Request::param('order_paytime_sta');
        $order_paytime_end  = Request::param('order_paytime_end');
        
        $where = [];
        $where[] = ['p.enable','=','1'];
        $where[] = ['p.type','=',1];
        $where[] = ['p.supplier_id','=',$s_uid];
        
        $where[] = ['o.enable','=','1'];
        $where[] = ['o.state','>','0'];
        if(!empty($order_paytime_sta)){
            $order_paytime_sta = strtotime($order_paytime_sta);
            $where[] = ['o.paytime','>=',$order_paytime_sta ];
        }
        if(!empty($order_paytime_end)){
            $order_paytime_end = strtotime($order_paytime_end)+24*60*60;
            $where[] = ['o.paytime','<',$order_paytime_end ];
        }
        if(!empty($p_name)){
//            $where[] = ['p_name','=',$p_name];
            $where[] = ['p.p_name','like',"%$p_name%"];
        }
        if(!empty($state)){
            $where[] = ['p.state','=',$state];
        }
        if($brand_state==-1){
            $brand_state=0;
            $where[] = ['p.brand_state','=',$brand_state];
        }elseif($brand_state==1){
            $where[] = ['p.brand_state','=',$brand_state];
        }
        
        if($brand_up_status==-1){
            $brand_up_state=0;
            $where[] = ['p.brand_up_status','=',$brand_up_status];
        }elseif($brand_up_status==1){
            $where[] = ['p.brand_up_status','=',$brand_up_status];
        }
        
        if($fs_up_status==-1){
            $fs_up_status=0;
            $where[] = ['p.fs_up_status','=',$fs_up_status];
        }elseif($fs_up_status==1){
            $where[] = ['p.fs_up_status','=',$fs_up_status];
        }
        
        if($pre_up_status==-1){
            $pre_up_state=0;
            $where[] = ['p.pre_up_status','=',$pre_up_status];
        }elseif($pre_up_status==1){
            $where[] = ['p.pre_up_status','=',$pre_up_status];
        }
        
        if($fs_state==-1){
            $fs_state=0;
            $where[] = ['p.fs_state','=',$fs_state];
        }elseif($fs_state==1){
            $where[] = ['p.fs_state','=',$fs_state];
        }
        
        if($pre_state==-1){
            $pre_state=0;
            $where[] = ['p.pre_state','=',$pre_state];
        }elseif($pre_state==1){
            $where[] = ['p.pre_state','=',$pre_state];
        }
        
        if(!empty($br_name)){
            $where[] = ['p.br_name','=',$br_name];
        }
        if(!empty($p_num)){
            $where[] = ['p.p_num','=',$p_num];
        }
        if(!empty($class_one)){
            $where[] = ['p.class_one','=',$class_one];
        }
        if(!empty($class_two)){
            $where[] = ['p.class_two','=',$class_two];
        }
        if(!empty($class_thr)){
            $where[] = ['p.class_thr','=',$class_thr];
        }
        if(!empty($supplier)){
            $where[] = ['p.supplier','=',$supplier];
        }
        if(!empty($con_person)){
            $where[] = ['p.con_person','=',$con_person];
        }
        if(!empty($con_information)){
            $where[] = ['p.con_information','=',$con_information];
        }
        if(!empty($res_person)){
            $where[] = ['p.res_person','=',$res_person];
        }
        if(!empty($cost_price_min) && !empty($cost_price_max)){
            $where[] = ['p.cost_price','>=',$cost_price_min];
            $where[] = ['p.cost_price','<=',$cost_price_max];
        }
        if(!empty($supply_price_min) && !empty($supply_price_max)){
            $where[] = ['p.supply_price','>=',$supply_price_min];
            $where[] = ['p.supply_price','<=',$supply_price_max];
        }

        $list = Product::productList($where,$page,$num);
        if(!empty($list)){
            $list = $list->toArray();
            foreach ($list as $key => $value) {
                $list[$key]["addtime"] = date( "Y-m-d H:i:s",$value["addtime"]);
                if(!empty($value["pre_sale_time"])){
                    $list[$key]["pre_sale_time"] = date( "Y-m-d H:i:s",$value["pre_sale_time"]);
                }
                if(!empty($value[$key]['img'])){
                    $list[$key]['img'] =  $value['img'];
                }
                    /*if(strpos($value['img'],"img.alicdn.com") !== false){
                        $storeList[$key]["img"] =  $value['img'];
                    }
                    elseif(strpos($value['img'],"http") !== false){
                        $storeList[$key]["img"] =  $value['img'];
                    }
                    else{
                        
                        $storeList[$key]["img"] =  Config::get('app.swimming_url').$value['img'];
                    }*/
            }
        }
        $count = Product::productCount($where);
        return json(['code' => 1, 'msg' => lang(''), 'data' => $list, 'count' => $count]);
    }
  
    
    //商品SKU列表
    public function proSKUList(){
        $s_uid = Request::param('s_uid');
        $pid = Request::param('pid');
        $page = Request::param('page');
        $num = Request::param('num');
        
        $where = [];
        $where[] = ['enable','=','1'];
        $where[] = ['type','=','5'];
        $where[] = ['tie_up_pid','=',$pid];
        $where[] = ['supplier_id','=',$s_uid];
        
        $list = Product::productList($where,$page,$num);
        if(!empty($list)){
            $list = $list->toArray();
            foreach ($list as $key => $value) {
                $list[$key]["addtime"] = date( "Y-m-d H:i:s",$value["addtime"]);
                if(!empty($value["pre_sale_time"])){
                    $list[$key]["pre_sale_time"] = date( "Y-m-d H:i:s",$value["pre_sale_time"]);
                }
                if(!empty($value[$key]['img'])){
                    $list[$key]['img'] =  $value['img'];
                }
                    /*if(strpos($value['img'],"img.alicdn.com") !== false){
                        $storeList[$key]["img"] =  $value['img'];
                    }
                    elseif(strpos($value['img'],"http") !== false){
                        $storeList[$key]["img"] =  $value['img'];
                    }
                    else{
                        
                        $storeList[$key]["img"] =  Config::get('app.swimming_url').$value['img'];
                    }*/
            }
        }
        $count = Product::productCount($where);
        return json(['code' => 1, 'msg' => lang(''), 'data' => $list, 'count' => $count]);
    }
    
 
    //商品详情
    public function productInfo(){
        $s_uid = Request::param('s_uid');

        $id = Request::param('id');
  
        $where = [];
        $where[] = ['id','=',$id];

        $info = Product::productInfo($where);
        if(!empty($info)){
            $info = $info->toArray();

            $info["addtime"] = date( "Y-m-d H:i:s",$info["addtime"]);
            if(!empty($info['img'])){
                $info['img'] =  $info['img'];
            }
            
            if(!empty($info["pre_sale_time"])){
                $info["pre_sale_time"] = date( "Y-m-d H:i:s",$info["pre_sale_time"]);
            }
            /*if(strpos($storeList['img'],"img.alicdn.com") !== false){
                $storeList["img"] =  $storeList['img'];
            }
            elseif(strpos($storeList['img'],"http") !== false){
                $storeList["img"] =  $storeList['img'];
            }
            else{

                $storeList["img"] =  Config::get('app.swimming_url').$storeList['img'];
            }*/
       
        }

        return json(['code' => 1, 'msg' => lang(''), 'data' => $info, 'count' => '' ]);
    }
    
    

   
    //物料详情
    public function materialInfo(){
        $s_uid = Request::param('s_uid');

        $id = Request::param('id');
  
        $where = [];
        $where[] = ['id','=',$id];

        $info = Product::productInfo($where);
        if(!empty($info)){
            $info = $info->toArray();

            $info["addtime"] = date( "Y-m-d H:i:s",$info["addtime"]);
            if(is_array($info['img_json'])){
                foreach ($info['img_json'] as $k => $v) {
                    if(strpos($info['img_json'][$k],"img.alicdn.com") !== false){
        //                $listinfo[$key]['commodity']['picture_url'][$k] =  Config::get('app.pomegranate_url').$value['commodity']['picture'][$k];
                        $info['img_json'][$k] =  $info['img_json'][$k];
                    }
                    elseif(strpos($info['img_json'][$k],"http") !== false){
                        $info['img_json'][$k] =  $info['img_json'][$k];
                    }
                    else{
                        $info['img_json'][$k] =  $info['img_json'][$k];
                    }
                }
            }

        }

        return json(['code' => 1, 'msg' => lang(''), 'data' => $info, 'count' => '' ]);
    }
  

}
